
import { checkUserLoginInfo, logout,getInfo } from '@/request/index'
export default {
    state: {
        isLogin: false, //  用户登录状态
        userInfoObj: {}, //  用户信息
        userIconInfoVisable: true,
        token: "",
        roles: [],
        permissions: [],

    },
    mutations: {
        /**
         * 保存登录状态
         * @param {object} state Vuex 的 state 对象
         * @param {boolean} data 登录状态
         */
        changeIsLogin(state, data) {
            state.isLogin = data
        },
        /**
         * 保存用户信息
         * @param {object} state Vuex 的 state 对象
         * @param {boolean} data 用户信息
         */
        changeUserInfoObj(state, data) {
            state.userInfoObj = Object.assign({}, state.userInfoObj, data)
        },
        /**
         * 用户信息按钮显示状态
         * @param {object} state Vuex 的 state 对象
         * @param {boolean} data 用户信息
         */
        changeUserIconInfoVisable(state, status) {
            state.userIconInfoVisable = status
        },
        setToken: (state, token) => {
            state.token = token
        },
        setName: (state, name) => {
            state.name = name
        },
        setRoles: (state, roles) => {
            state.roles = roles
        },
        setPermissions: (state, permissions) => {
            state.permissions = permissions
        },
    },
    actions: {
        /**
         * 获取用户信息
         */
        getUserInfo(context) {
            return checkUserLoginInfo().then((res) => {
                if (res.code == 200 && res.data.user) {
                    // 改变登录状态
                    context.commit('changeIsLogin', true)
                    // 保存用户信息
                    context.commit('changeUserInfoObj', res.data.user)
                } else {
                    context.commit('changeIsLogin', false)
                }
            })
        },
        // 退出系统
        LogOut({ commit, state }) {
            return logout()
                    .then(() => {
                        uni.removeStorageSync('token');
                        commit('setToken', '');
                        commit('setRoles', []);
                        commit('setPermissions', []);
                        commit('changeCurrentTabbar', "数字看板")
                        console.log(this.state.user.token, this.state)
                    })
                    .catch((error) => {
                        console.log(error);
                    });
           
        },
        // 获取用户信息
        GetInfo(context) {
            return getInfo().then(res => {
                const data = res.data 
                
                if (res.code == 200 && data.user != null) {
                    // const avatar = (user.avatar == "" || user.avatar == null) ? require("@/assets/images/profile.jpg") : process.env.VUE_APP_BASE_API + user.avatar;
                    if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
                        context.commit('setRoles', data.roles)
                        context.commit('setPermissions', data.permissions)
                    } else {
                        context.commit('setRoles', ['ROLE_DEFAULT'])
                    }
                    // 改变登录状态
                    context.commit('changeIsLogin', true)
                    context.commit('setName', data.user.userName)
                    context.commit('changeUserInfoObj', res.data.user)
                    // commit('setAvatar', avatar)
                    // resolve(res)
                } else {
                    context.commit('changeIsLogin', false)
                }

            }).catch(error => {
                // 改变登录状态
                context.commit('changeIsLogin', false)
                // reject(error)
            })

        },
    }
}
